//
//  UIImage+Ext.swift
//  KLExtension
//
//  Created by nkl on 2023/6/29.
//

import Foundation

extension UIImage {
    
    func changeColor(color: UIColor) -> UIImage {
        guard let cgImage = cgImage else {
            return self
        }

        let context = CIContext(options: nil)
        let coreImage = CIImage(cgImage: cgImage)

        let filter = CIFilter(name: "CIColorMonochrome")
        filter?.setValue(coreImage, forKey: kCIInputImageKey)

        let ciColor = CIColor(color: color)
        filter?.setValue(ciColor, forKey: kCIInputColorKey)
        filter?.setValue(1.0, forKey: kCIInputIntensityKey)

        if let outputImage = filter?.outputImage,
           let cgImageOutput = context.createCGImage(outputImage, from: outputImage.extent) {
            let processedImage = UIImage(cgImage: cgImageOutput)
            return processedImage
        }
        return self
    }
}
